<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar> </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.parent.code })">
          {{ record.parent.code }}
        </a>
      </template>
      <template #summary>
        <TableSummary fixed>
          <TableSummaryRow>
            <TableSummaryCell :index="0" :col-span="2" align="center"> 合计： </TableSummaryCell>
            <TableSummaryCell :index="2" :col-span="15"> </TableSummaryCell>
            <TableSummaryCell :index="17" align="right">
              {{ iqty }}
            </TableSummaryCell>
            <TableSummaryCell :index="18" align="right">
              {{ fweight }}
            </TableSummaryCell>
            <TableSummaryCell :index="19" :col-span="24"> </TableSummaryCell>
          </TableSummaryRow>
        </TableSummary>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsWhRd01Rd01ListDetails">
  import { unref, watch, onMounted, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rd01Delete, rds01ListData } from '/@/api/wms/rd/rd01/Rdrecord01';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { TableSummary, TableSummaryCell, TableSummaryRow } from 'ant-design-vue';

  const { t } = useI18n('wh.rd01.rd01');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('采购入库管理'),
  };

  const props = defineProps({
    code: { type: String, default: '' },
  });

  const iqty = computed(() => {
    let sumiqty = 0;
    for (const record of getDataSource()) {
      const { iqty } = record;
      sumiqty += Number(iqty || 0);
    }
    return sumiqty.toFixed(2);
  });

  const fweight = computed(() => {
    let sumfweight = 0;
    for (const record of getDataSource()) {
      const { fweight } = record;
      sumfweight += Number(fweight || 0);
    }
    return sumfweight.toFixed(4);
  });

  watch(
    () => props.code,
    async () => {
      await getForm().setFieldsValue({
        'parent.code': props.code,
      });
      await reload();
    },
  );

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('入库单号'),
        field: 'parent.code',
        component: 'Input',
      },
      {
        label: t('供应商'),
        field: 'parent.venName',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'parent.ddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('来源单号'),
        field: 'parent.sourceCode',
        component: 'Input',
      },
      {
        label: t('单据状态'),
        field: 'parent.djStatus',
        component: 'Select',
        componentProps: {
          dictType: 'sys_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('仓库'),
        field: 'parent.basWare.cwhname',
        component: 'TreeSelect',
        componentProps: {},
      },
      {
        label: t('商品编码'),
        field: 'basInv.invCode',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('商品名称'),
        field: 'basInv.invName',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('形象刊'),
        field: 'cfree1',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('外部系统单号'),
        field: 'extCode',
        component: 'Input',
      },
      {
        label: t('外部系统结果'),
        field: 'extRs',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['parent.ddate', ['parent.ddate_gte', 'parent.ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公司'),
      dataIndex: 'parent.company.companyName',
      key: 'a.corp_code',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('入库单号'),
      dataIndex: 'parent.code',
      key: 'a.code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('供应商'),
      dataIndex: 'parent.venName',
      key: 'a.ven_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单据日期'),
      dataIndex: 'parent.ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('预计到达日期'),
      dataIndex: 'parent.preArrDate',
      key: 'a.pre_arr_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('来源单号'),
      dataIndex: 'parent.sourceCode',
      key: 'a.source_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('仓库'),
      dataIndex: 'parent.basWare.cwhname',
      key: 'a.wh_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('部门'),
      dataIndex: 'parent.officeName',
      key: 'a.office_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单时间'),
      dataIndex: 'parent.createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'parent.createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注信息'),
      dataIndex: 'parent.remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单据状态'),
      dataIndex: 'parent.djStatus',
      key: 'a.dj_status',
      dictType: 'sys_status',
      sorter: true,
      width: 100,
      align: 'center',
      fixed: 'left',
    },
    {
      title: t('审批状态'),
      dataIndex: 'parent.status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
    {
      title: t('商品编码'),
      dataIndex: 'basInv.invCode',
      key: 'inv.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('商品名称'),
      dataIndex: 'basInv.invName',
      key: 'inv.inv_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.funitname',
      key: 'inv.funitname',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('货位'),
      dataIndex: 'basPosition.posName',
      key: 'pos.pos_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('重量'),
      dataIndex: 'fweight',
      key: 'a.fweight',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('形象刊'),
      dataIndex: 'cfree1',
      key: 'a.cfree1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('行备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('外部系统单号'),
      dataIndex: 'extCode',
      key: 'a.ext_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('外部系统结果'),
      dataIndex: 'extRs',
      key: 'a.ext_rs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('外部系统时间'),
      dataIndex: 'extDate',
      key: 'a.ext_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑采购入库'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'wh:rd01:rd01:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除采购入库'),
        popConfirm: {
          title: t('是否确认删除采购入库'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wh:rd01:rd01:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'i-fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource }] = useTable({
    api: rds01ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await rd01Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'flow_wh_rd01', bizKey: record.id });
  }

  onMounted(async () => {
    await getForm().setFieldsValue({
      'parent.code': props.code || '',
    });
    await reload();
    await getForm().setFieldsValue({
      'parent.code': props.code || '',
    });
  });
</script>
